/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.databox.quickfinder;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import twaver.Alarm;
import twaver.AlarmModelQuickFinder;
import twaver.DataBoxQuickFinder;
import twaver.Element;
import twaver.ElementCallbackHandler;
import twaver.Node;
import twaver.TDataBox;

public class QuickFinderDemo {
    
    static TDataBox box = new TDataBox ();
    
    public static void addNode (String name, int age) {
        Node node = new Node (name) {
            public String toString () {
                return this.getName ();
            }
        };
        node.setName (name);
        box.addElement (node);
        node.putClientProperty ("age", new Integer (age));
    }
    
    public static void addAlarm(String id, int value){
    	Alarm alarm = new Alarm(id){
            public String toString () {
                return this.getAlarmID().toString();
            }
    	};
    	alarm.putClientProperty("alarmKey", value);
    	box.getAlarmModel().addAlarm(alarm);
    	
    }
    
    public static void main (String[] args) {
        addNode ("A", 12);
        addNode ("B", 12);
        addNode ("C", 12);

        addNode ("D", 15);
        addNode ("E", 15);
        addNode ("F", 15);
        
        DataBoxQuickFinder finder = box.createClientPropertyFinder("age");        
        System.out.println (finder.find (new Integer (12)));
        
        box.getElementByID ("B").putClientProperty ("age", new Integer (20));
        System.out.println (finder.find (new Integer (12)));
        System.out.println (finder.find (new Integer (20)));
        
        box.removeElement (box.getElementByID ("D"));
        System.out.println (finder.find (new Integer (15)));
        
        Node node;
        for(int i=0;i<=50;i++) {
            node = new Node("guy"+i);
            node.setUserObject("user "+i);
            box.addElement(node);
        }

        finder = box.createJavaBeanFinder("userObject");
        
        List list = finder.find("user 9");
        Iterator iterator = list.iterator();
        while(iterator.hasNext()) {
            node = (Node)iterator.next();
            System.out.println("the correct element'ID is :"+node.getID());
        }

        
        finder = box.createUserPropertyFinder("salary");
        box.iterator(new ElementCallbackHandler(){
			public boolean processElement(Element element) {
				element.putUserProperty("salary", new Integer(9999));
				return true;
			}
        });
        
        list = finder.find(new Integer(9999));
        System.out.println("box's size is " + box.size() + ", " + list.size() + " elements with salary 9999");
       
        list = new ArrayList(list);
        for(int i=0; i<list.size()/2; i++){
        	Element element = (Element)list.get(i);
        	element.putUserProperty("salary", new Integer(8888));
        }
        // copy new Collection
        list = finder.find(new Integer(8888)); 
        System.out.println(list.size() + " elements with salary 8888");
        
        AlarmModelQuickFinder alarmFinder = box.getAlarmModel().createAlarmFinder("alarmKey");
        addAlarm("A1", 100);
        addAlarm("A2", 100);
        addAlarm("A3", 100);
        addAlarm("A4", 200);
        addAlarm("A5", 200);
        addAlarm("A6", 300);
        System.out.println (alarmFinder.find (new Integer (100)));
        System.out.println (alarmFinder.find (new Integer (200)));
        System.out.println (alarmFinder.find (new Integer (300)));
        box.getAlarmModel().getAlarmByID("A6").putClientProperty("alarmKey", 100);
        box.getAlarmModel().removeAlarmByID("A2");
        System.out.println (alarmFinder.find (new Integer (100)));
    }
}